home *** CD-ROM | disk | FTP | other *** search
- // This may look like C code, but it is really -*- C++ -*-
- //
- // Verify construction of fractal maps and show them off
- //
- // $Id: vfractal_map.cc,v 2.0 1995/03/21 18:40:16 oleg Exp oleg $
-
- #include "fractal_map.h"
- #include "filter.h"
- #include <iostream.h>
- #include <math.h>
-
- #ifndef M_PI
- #define M_PI _PI
- #endif
-
- static void test_nonoise(const card order)
- {
- cout << "\nTest constructing a map of order " << order <<
- " without any noise\n" << endl;
-
- class NonNoise : public FractalMap
- {
- public:
- NonNoise(const card order, const Seeds& seeds = FractalMap::Seeds(128),
- const bits_per_pixel=8)
- : FractalMap(order,seeds,bits_per_pixel) {}
- inline int get_noise(const card scale) const { return 0; }
- };
-
- {
- const card seed = 128;
- cout << "\tUniform seed " << seed << endl;
- IMAGE map = NonNoise(order,seed,8);
- verify_pixel_value(map,seed);
- }
-
- {
- const card seed = 128; // Must be a power of 2
- cout << "\tHorizontal gradient fill, seed " << seed << endl;
- IMAGE map = NonNoise(order,FractalMap::Seeds(0,0,seed,seed),8);
- class CheckMap : public PixelAction
- {
- double step;
- void operation(GRAY& pixel)
- {
- const GRAY expected = (GRAY)(col * step);
- if( col != ncols-1 && pixel != expected )
- _error("at (%d,%d), expected %d, found %d",
- row,col,expected,pixel);
- }
- public: CheckMap(const card seed, const card order) :
- step((double)seed/(1<<order)) {}
- };
-
- // map.print("map"); exit(0);
- map.apply(CheckMap(seed,order));
- }
-
- {
- const card seed = 128; // Must be a power of 2
- cout << "\tVertical gradient fill, seed " << seed << endl;
- IMAGE map = NonNoise(order,FractalMap::Seeds(0,seed,0,seed),8);
- class CheckVMap : public PixelAction
- {
- double step;
- void operation(GRAY& pixel)
- {
- const GRAY expected = (GRAY)(row * step);
- if( row != nrows-1 && pixel != expected )
- _error("at (%d,%d), expected %d, found %d",
- row,col,expected,pixel);
- }
- public: CheckVMap(const card seed, const card order) :
- step((double)seed/(1<<order)) {}
- };
-
- // map.print("map"); exit(0);
- map.apply(CheckVMap(seed,order));
- }
-
- cout << "\nDone" << endl;
- }
-
- static void test_norandom(void)
- {
- const card order = 2;
- cout << "\nTest constructing a map of order " << order <<
- " with non-random noise\n" << endl;
-
- class NoRNoise : public FractalMap
- {
- public:
- NoRNoise(const card order, const Seeds& seeds = FractalMap::Seeds(128),
- const bits_per_pixel=8)
- : FractalMap(order,seeds,bits_per_pixel) {}
- inline int get_noise(const card scale) const { return scale/2; }
- };
-
- const card seed = 0;
- cout << "\tUniform seed " << seed << endl;
- IMAGE map = NoRNoise(order,seed,8);
- map.print("map");
-
- IMAGE expected(map);
- expected = 2; expected(0,0) = 0;
- expected.square_of(3,rowcol(1,1)) = 4;
- assert( map == expected );
-
- cout << "\nDone" << endl;
- }
-
- // Show off maps
- static void demo_maps(void)
- {
- cout << "Showing off different maps" << endl;
-
- class GaussNoise : public FractalMap
- {
- public:
- GaussNoise(const card order, const Seeds& seeds,
- const bits_per_pixel=8)
- : FractalMap(order,seeds,bits_per_pixel) {}
- // Well-known Gaussian random number generator
- // Note rand() returns a number [0,2^15-1],
- // that is, within [0,1] with 15-bit implicit
- // fraction
- inline int get_noise(const card scale) const {
- long sum = 0;
- for(register int i=0; i<12; i++)
- sum += rand(); // keep the result within
- return (scale * (sum-(6<<15)))>>17; } // [-scale/2,scale/2]
- };
-
- LookupT sine_trans(LookupT::Identical(8)); // For some visual effect
- {
- for(register int i=sine_trans.q_min(); i<=sine_trans.q_max(); i++)
- sine_trans[i] = (GRAY)((sin((i * M_PI)/128)+1)*128 - 1);
- }
-
- for(;;)
- {
- cout << "Enter the map's order (log2 of map's dimension) (0 to quit)"
- << endl;
- int order;
- cin >> order;
- if( order <= 1 )
- return;
- cout << "Select random number generator: 0 - Uniform, 1 - Gaussian"
- << endl;
- int type;
- cin >> type;
- cout << "Enter the four seeds "
- "(at upper-left, lower-left, upper-right, lower-right corners)" << endl;
- int sul, sll, sur, slr;
- cin >> sul >> sll >> sur >> slr;
- IMAGE map = type == 0 ?
- (LazyImage&)FractalMap(order,FractalMap::Seeds(sul,sll,sur,slr)) :
- (LazyImage&)GaussNoise(order,FractalMap::Seeds(sul,sll,sur,slr));
- cout << "Select post-processing option" << endl;
- cout << "\t0 - no postprocessing" << endl;
- cout << "\t1 - apply blurring" << endl;
- cout << "\t2 - apply a sine function" << endl;
- cin >> type;
- if( type == 1 )
- FilterIt(map).conv(conv_kernel(1,1,1,CommonDenom(3))).
- clip_to_intensity_range();
- else if( type == 2 )
- FilterIt(map).translate(sine_trans, LookupT::CoerceFringes);
-
- char buffer[70];
- sprintf(buffer,"Seeds(%d,%d,%d,%d)",sul,sll,sur,slr);
- // map.write_pgm("/tmp/abc",buffer);
- // system("/usr/local/bin/X11/xv /tmp/abc");
- map.display(buffer);
- }
- }
-
- main(void)
- {
- test_nonoise(4);
- test_nonoise(9);
- test_norandom();
-
- #ifdef __MWERKS__ // Macintosh
- GetDateTime((unsigned long *)&qd.randSeed);
- Random(); Random(); Random(); Random(); // Just randomizing
- #else
- srand(time(0));
- rand(); rand(); rand(); rand();
- #endif
-
- demo_maps();
- }
-